package executors.completable.future;

import java.util.concurrent.*;

public class AllOfTest001 {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    
    CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
      System.out.println("线程B");
      return "B";
    }, executorService);
    
    CompletableFuture.allOf(CompletableFuture.runAsync(() -> {
      System.out.println("线程A");
    }, executorService), stringCompletableFuture, CompletableFuture.runAsync(() -> {
      System.out.println("线程C");
    }, executorService)).thenAcceptAsync((res) -> {
      System.out.println("这是执行结果: " + res + ", " + Thread.currentThread().getName());
      System.out.println("执行最终结果");
    }, executorService);
    
    System.out.println(stringCompletableFuture.get() + " | " + Thread.currentThread().getName());
  }
}
